import pygame
from pygame.locals import *
from pygame import *
from guiinput import guiinput
from classes import *

class message(pygame.sprite.Sprite):
    def __init__(self, messages, application):
        pygame.sprite.Sprite.__init__(self)
        self.messages = []
        self.buttons = []
        self.select = None
        self.number = None
        self.frame = []
        self.messagelist = []
        # FIRST WE CALC THE SIZE OF ALL MESSAGES, EG. WE USE A RESOLUTION OF 800x600 AND WANT TO SHOW 3 MESSAGES:
        # 3 MESSAGES ARE 3*25PX=75PX (25 = 600 / 24)
        # NOW REMOVE THE HEIGHT OF ALL MESSAGES FROM THE RESOLUTION HEIGHT AND DIV. THE REST BY 2
        # 600 - 75 = 525 / 2 = 262 -> STARTING POINT FOR OUR 1ST MESSAGE (msy)
        maxxl = 0 # MAX LENGTH FOR THE LARGEST MESSAGE
        may = application.cubex * len(messages) # HEIGHT FOR ALL MESSAGES
        msy = (application.resolution[1] - may) / 2 # STARTING POINT
        # RENDER ALL MESSAGES AND DEFINE Y POSITIONS
        for x in range(0, len(messages)):
            messages[x] = ' ' + messages[x] + ' '
            self.messages.append(loadlabel(messages[x], application.fontsize, application.fontcolor, application.font, application.fontbold, application.fontitalic))
            self.messages[x].rect.y = msy
	    #print application.themepath
            # NOW WE NEED THE MAX X SIZE OF THE MESSAGE WINDOW
            if self.messages[x].rect.width > maxxl: maxxl = self.messages[x].rect.width
            self.buttons.append(loadimage(application.themepath + '/widgets/B.png', application.cubex, application.cubex))
            self.buttons[x].rect.y = msy
            msy += application.cubex
        # DEFINE ALL X POSITIONS - WE CAN THAT DO NOW, BECAUSE WE HAVE NOW maxxl, THE MAX LENGTH
        for x in range(0, len(messages)):
            self.messages[x].rect.x = ((application.resolution[0] - maxxl) / 2) + application.cubex / 2
            self.buttons[x].rect.x = ((application.resolution[0] - maxxl) / 2) - application.cubex / 2
        # WE HAVE LOADED ALL MESSAGES AND BUTTONS, NOW CHECK IF THERES ONLY ONE MESSAGE
        # SO WE CAN CENTER THE BUTTON (OR NOT)
        if len(messages) == 1:
            self.messages[0].rect.x = application.resolution[0] / 2 - self.messages[0].rect.width / 2
            self.messages[0].rect.y = application.resolution[1] / 2 - self.messages[0].rect.height / 2 - self.buttons[0].rect.height / 2
            self.buttons[0].rect.x = self.messages[0].rect.x + (self.messages[0].rect.width / 2) - (self.buttons[0].rect.width / 2) 
            self.buttons[0].rect.y = self.messages[0].rect.y + self.messages[0].rect.height
        # LOAD BACKGROUND
        if len(messages) == 1:
            background = loadimage(application.themepath + '/widgets/message_background.png', self.messages[0].rect.width, self.messages[0].rect.height + self.buttons[0].rect.height)
            background.rect.x = self.messages[0].rect.x
            background.rect.y = self.messages[0].rect.y
        else:
            background = loadimage(application.themepath + '/widgets/message_background.png', application.cubex+maxxl ,len(messages)*application.cubex)
            background.rect.x = self.buttons[0].rect.x
            background.rect.y = self.buttons[0].rect.y
        # LOAD SELECTION BUTTON
        button = loadimage(application.themepath + '/widgets/A.png', self.buttons[0].rect.width, self.buttons[0].rect.height)
        button.rect.x = self.buttons[0].rect.x
        button.rect.y = self.buttons[0].rect.y
        s = 0
        # LOAD FRAME
        self.frame.append(loadimage(application.themepath + '/widgets/frame_upperleft.png', application.cubex / 2, application.cubex / 2))
        self.frame[0].rect.x = background.rect.x - application.cubex / 2
        self.frame[0].rect.y = background.rect.y - application.cubex / 2
        self.frame.append(loadimage(application.themepath + '/widgets/frame_upperright.png', application.cubex / 2, application.cubex / 2))
        self.frame[1].rect.x = background.rect.x + background.rect.width
        self.frame[1].rect.y = background.rect.y - application.cubex / 2
        self.frame.append(loadimage(application.themepath + '/widgets/frame_bottomleft.png', application.cubex / 2, application.cubex / 2))
        self.frame[2].rect.x = background.rect.x - application.cubex / 2
        self.frame[2].rect.y = background.rect.y + background.rect.height
        self.frame.append(loadimage(application.themepath + '/widgets/frame_bottomright.png', application.cubex / 2, application.cubex / 2))
        self.frame[3].rect.x = background.rect.x + background.rect.width
        self.frame[3].rect.y = background.rect.y + background.rect.height
        self.frame.append(loadimage(application.themepath + '/widgets/frame_upper.png', background.rect.width, application.cubex / 2))
        self.frame[4].rect.x = background.rect.x
        self.frame[4].rect.y = background.rect.y - application.cubex / 2
        self.frame.append(loadimage(application.themepath + '/widgets/frame_bottom.png', background.rect.width, application.cubex / 2))
        self.frame[5].rect.x = background.rect.x
        self.frame[5].rect.y = background.rect.y + background.rect.height
        self.frame.append(loadimage(application.themepath + '/widgets/frame_left.png', application.cubex / 2, background.rect.height))
        self.frame[6].rect.x = background.rect.x - application.cubex / 2
        self.frame[6].rect.y = background.rect.y
        self.frame.append(loadimage(application.themepath + '/widgets/frame_right.png', application.cubex / 2, background.rect.height))
        self.frame[7].rect.x = background.rect.x + background.rect.width
        self.frame[7].rect.y = background.rect.y
        self.messagelist.append(background)
        self.messagelist.extend(self.buttons)
        self.messagelist.append(button)
        self.messagelist.extend(self.messages)
        self.messagelist.extend(self.frame)
        messagegrouprender = pygame.sprite.OrderedUpdates(self.messagelist)
        messagegrouprender.draw(pygame.display.get_surface())
        pygame.display.flip()
        # WAIT FOR INPUT
        device = 'any'
        key = guiinput()
        while 1:
            key.getkey()
            # UP
            if key.keyname == 'down':
                if s > 0:
                    button.rect.y -= button.rect.height
                    s -= 1
                    messagegrouprender.draw(pygame.display.get_surface())
                    pygame.display.flip()
            # DOWN
            if key.keyname == 'up':
                if s < len(messages) -1:
                    button.rect.y += button.rect.height
                    s += 1
                    messagegrouprender.draw(pygame.display.get_surface())
                    pygame.display.flip()
            # SELECT
            if key.keyname == 'a':
                messagelist = None
                application.spritegrouprender.draw(pygame.display.get_surface())
                pygame.display.flip()
                self.number = s
                self.select = messages[s][1:-1]
                break
            # ABORT
            if key.keyname == 'b':
                messagelist = None
                application.spritegrouprender.draw(pygame.display.get_surface())
                pygame.display.flip()
                break
            key.delete()
